define([], function() {
	return {
		title: ['<a href="user.html">个人中心</a>', "申请店铺"],
		init: function(content) {

			let template = content.find(".image-item._template").clone();

			let gisDiv = content.children("._gis");
			console.info(gisDiv);

			window.frames.gis.addEventListener("map-select", function(e) {
				$("._lng").val(e.data.lng);
				$("._lat").val(e.data.lat);
			});

			content.find("._gis").on("click", function() {
				gisDiv.toggle();
			});

			content.on("click", ".image-item", function(e) {
				let btn = $(e.target);
				if(btn.is("button")) {
					btn.parent().children("[type='file']").click();
				} else if(btn.is("._add")) {
					btn.parents(".image-item").before(template.clone());
				}
			});

			content.on("change", ".image-item", function(e) {
				let imgFile = $(e.target);
				if(imgFile.is("[type='file']")) {
					let img = imgFile.parent().children("img");
					var f = imgFile[0].files[0];
					var reader = new FileReader();
					reader.onload = function() {
						img[0].src = reader.result;
					}
					reader.readAsDataURL(f);
				}
			});

			var form = content.children("form");
			var progress = content.find(".progress");
			var submitBtn = form.find(":submit");
			form.on("submit", function() {
				submitBtn.attr("disabled","disabled");
				let formData = new FormData(form[0]);

				progress.show();

				$.ajax({
					type: "post",
					url: "shops/add.do",
					processData: false, // 不会将 data 参数序列化字符串
					contentType: false, // 根据表单 input 提交的数据使用其默认的 contentType
					data: formData,
					success: function(data) {
						submitBtn.removeAttr("disabled");
						if(data.ok) {
							toastr.success("申请成功");
							window.location="user.html";
						} else {
							toastr.error(data.message);
						}
						progress.hide();
					},
					dataType: "json",
					xhr: function() {
						var xhr = new window.XMLHttpRequest();
						xhr.upload.addEventListener("progress", function(evt) {
							if(evt.lengthComputable) {
								var percentComplete = evt.loaded * 100 / evt.total;
								progress.children().css("width", percentComplete + "%");
							}
						}, false);
						return xhr;
					}
				});

				return false;
			});

		}
	}
});